package com.work.device.service.impl;

import com.work.device.mapper.DeviceInfoMapper;
import com.work.device.mapper.IndexMapper;
import com.work.device.pojo.DeviceInfo;
import com.work.device.pojo.Index;
import com.work.device.service.DeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DeviceInfoServiceImpl implements DeviceInfoService {

    @Autowired
    private DeviceInfoMapper deviceInfoMapper;
    @Autowired
    private IndexMapper indexMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据设备id查询数据
     *
     * @param id 设备id
     * @return 设备信息
     */
    @Override
    public List<DeviceInfo> findInfoById(Long id) {

        List<DeviceInfo> deviceInfoList = new ArrayList<>();
        //t_device_index
        //首先查询设备id数据指标集合
        List<Long> deviceAndIndexIds = deviceInfoMapper.findDeviceAndIndexIdByDeviceId(id);
        if (deviceAndIndexIds != null && deviceAndIndexIds.size() > 0) {
            for (Long deviceAndIndexId : deviceAndIndexIds) {
                //deviceInfo
                DeviceInfo deviceInfo = new DeviceInfo();
                deviceInfo.setDid(id);
                //t_info param info
                Map<String, Object> infoMap = deviceInfoMapper.findInfoByDeviceId(id);
                deviceInfo.setParam((String) infoMap.get("param"));
                deviceInfo.setInfo((String) infoMap.get("info"));
                deviceInfo.setId((Long) infoMap.get("id"));

                Map<String, Object> indexMap = deviceInfoMapper.findDeviceAndIndexById(deviceAndIndexId);
                if (indexMap != null && indexMap.size() > 0) {
                    Object idObj = indexMap.get("id");
                    if (idObj != null) {
                        deviceInfo.setDiid((Long) idObj);
                    }
                    Object rangeObj = indexMap.get("range");
                    if (rangeObj != null) {
                        deviceInfo.setRange((String) rangeObj);
                    }
                    Object accuracyObj = indexMap.get("accuracy");
                    if (accuracyObj != null) {

                        deviceInfo.setAccuracy((String) accuracyObj);
                    }
                    Object resolving_powerObj = indexMap.get("resolving_power");
                    if (resolving_powerObj != null) {

                        deviceInfo.setResolvingPower((String) resolving_powerObj);
                    }

                    Long iid = (Long) indexMap.get("iid");
                    deviceInfo.setIid(iid);
                    Index index = indexMapper.selectByPrimaryKey(iid);
                    deviceInfo.setUnit(index.getUnit());
                    deviceInfo.setDataName(index.getDataName());
                }
                deviceInfo.setIid(null);
                deviceInfo.setId(null);
                deviceInfoList.add(deviceInfo);

            }

        } else {
            //deviceInfo
            DeviceInfo deviceInfo = new DeviceInfo();
            deviceInfo.setDid(id);
            //t_info param info
            Map<String, Object> infoMap = deviceInfoMapper.findInfoByDeviceId(id);
            deviceInfo.setParam((String) infoMap.get("param"));
            deviceInfo.setInfo((String) infoMap.get("info"));
            deviceInfo.setId((Long) infoMap.get("id"));

            deviceInfoList.add(deviceInfo);
        }

        return deviceInfoList;
    }

    /**
     * 更新设备使用说明
     *
     * @param deviceInfo 设备信息
     * @return 数据库更新的行数
     */
    @Override
    @Transactional
    public Integer update(DeviceInfo deviceInfo) {
        //设备参数和设备信息
        Integer infoResult = deviceInfoMapper.updateInfo(deviceInfo);
        if (deviceInfo != null && deviceInfo.getDataName() != null) {
            //设备数据与数据指标关联表
            Example example = new Example(Index.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("dataName", deviceInfo.getDataName());
            List<Index> indices = indexMapper.selectByExample(example);
            //数据指标是否改变
            Map deviceAndIndex = deviceInfoMapper.findDeviceAndIndexById(deviceInfo.getDiid());
            if (deviceAndIndex != null && deviceAndIndex.size() > 0) {
                //数据指标没有改变
                if (((Long) deviceAndIndex.get("iid")).longValue()==((Long)indices.get(0).getId()).longValue()) {
                    //更新设备详情
                    deviceInfoMapper.updateDeviceAndIndex(deviceInfo);
                } else {
                    //数据指标是否冲突
                    //根据设备did和数据指标iid查询数据库是否已经存在
                    Integer exist = deviceInfoMapper.exist(deviceInfo.getDid(), indices.get(0).getId());
                    if (exist == 0) {
                        //如果不存在,更新设备
                        deviceInfo.setIid(indices.get(0).getId());
                        deviceInfoMapper.updateDeviceAndIndex(deviceInfo);
                    } else {
                        //如果存在
                        throw new RuntimeException("数据库已经存在该设备和数据指标的信息,非法操作");
                    }
                }
            }
        }
        //返回更新结果
        return infoResult;
    }
}
